import { ref } from 'vue'
import { adapter, request } from '@/request'
import { activityListByGet } from '@/core/api/activity'
import type { Activity } from '@/core/interfaces/activity'
import type { ActivityTypeEnum } from '@/core/enums/type'
import type { ActivityStatusEnum } from '@/core/enums/status'
import type { Pagination, RequestWrapper, WithTotalSize } from '@/core/interfaces/common'

interface Params extends Pagination {
  type: ActivityTypeEnum
  status: ActivityStatusEnum
  startTime: Activity['startTime']
  endTime: Activity['endTime']
}

export default function getActivityList() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'activities', Activity[]>>>>(request)
  const loading = ref(false)
  const response = ref<Activity[]>([])

  const fetch = (params: Params) => {
    const { url, method } = activityListByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data.activities
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
